<?php

class ErrorController extends Zend_Controller_Action {
	
	public function init() {
		Manager::getPageCache()->cancel();
		$this->view->title = "error.title";
		$this->view->message = 'Application error';
		$this->view->errors = null;
		$this->view->errorMessage = null;

		if ('development' == APPLICATION_ENV) {
			$this->_helper->layout->disableLayout();
		}
	}
	
	public function errorAction() {
		$errorHandlers = array();
		if (!is_null($this->_getParam('error_handler'))) {
			array_push($errorHandlers, $this->_getParam('error_handler'));
		}
		$errorPlugins = $this->_getParam(Plugins_ErrorPlugin::paramError, array());

		$this->view->errors = array_merge($errorHandlers, $errorPlugins);
		$this->view->errors = array_reverse($this->view->errors);

		$errors = end($this->view->errors);

		// Traitement pour la page
		if ($errors) {
			switch ($errors->type) {
				case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
				case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
				case Plugins_ErrorPlugin::EXCEPTION_NO_LANGAGE:
				case Plugins_ErrorPlugin::EXCEPTION_NO_PHOTOS:
					// 404 error -- controller or action or language or photos not found
					$this->getResponse()->setHttpResponseCode(404);
					$this->view->message = 'Page not found';
					$this->view->errorMessage= 'No controller or action or language or photos found';
					$this->view->userMessage= 'error.page.not.found';
					break;
				default:
					$this->view->userMessage= 'error.internal';
					switch (get_class($errors->exception)) {
						case 'Zend_View_Exception' :
							$this->getResponse()->setHttpResponseCode(500);
							$this->view->errorMessage = 'View processing error';
							break;
						case 'Zend_Db_Exception' :
							$this->getResponse()->setHttpResponseCode(503);
							$this->view->errorMessage = 'Database processing error';
							break;
						case 'Models_ExceptionModels' :
							$this->getResponse()->setHttpResponseCode(200);
							$this->view->errorMessage = 'Application error';
							break;
						default:
							$this->getResponse()->setHttpResponseCode(500);
							$this->view->errorMessage = 'Unknown error';
							break;
					}
					break;
			}
		}
	}
}
